<?php
/**
 * @package panel
 * @subpackage panel.rrp
**/
/**
 * @author Dmitry Bakaleinik (code)
 */
abstract class panel__rrp
	implements maf__mvc__i__controller {

	/**
	 * Name of controller
	 * @var string
	 */
	protected $title = "";

	/**
	 * Routing/map function
	 * @return array
	 */
	abstract public function __route();

	/**
	 * Processing function
	 * @throws panel__e__unknown_processor_action
	 * @throws panel__e__unsupported_catalog
	 * @throws panel__e__exception
	 * @param string $_action_
	 * @param array  $_request_
	 * @param panel__application__response $_response_
	 * @return mixed
	 */
	public function process() {

		$_action_   = func_get_arg(0);
		$_action_   = p()->validate($_action_, "string!", "Processor Action");

		$_request_  = func_get_arg(1);
		$_response_ = func_get_arg(2);

		// Collect methods
		$methods = $this->__route();

		if( !is_array($methods) ) {
			throw new panel__e__exception("Route function is broken, required array as return value."); }

		// Search default and prepare
		foreach( $methods as $_k => &$_m ) {

			if( !is_array($_m) ) {
				$_m = array(); }

			$_m["call"] = isset_or($_m["call"], $_k);

			if( isset_or($_m["is_default"]) ) {
				$default_method = $_k; }

		}	# foreach

		if( isset($methods[$_action_]) ) {

			$method = $methods[$_action_];

		} else if( isset($default_method) && $_action_ == "" ) {

			$method = $methods[$default_method];

		} else {

			throw new panel__e__unknown_processor_action($_action_);

		}	# if

		if( !method_exists($this, $method["call"]) ) {

			throw new panel__e__unknown_processor_action(
				$_action_,
				"Method '{$method["call"]}' is not found."
			);

		}	# if

		// Call
		return $this->$method["call"]($_request_, $_response_);

	}	# Function

}	# Class

/* EOF */